package com.basics.common.utils;


import java.nio.charset.Charset;
import java.util.UUID;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

public class GuidUtils {
    private static final String CHARSET_NAME = "UTF-8";

    public GuidUtils() {
    }

    public static String generateDefaultGuid() {
        return UUID.randomUUID().toString();
    }

    public static String generateSimpleGuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public static long generateDefaultLongGuid() {
        return generateCheckSumCRC32(generateDefaultGuid().getBytes(Charset.forName("UTF-8")));
    }

    public static long generateSimpleLongGuid() {
        return generateCheckSumCRC32(generateSimpleGuid().getBytes(Charset.forName("UTF-8")));
    }

    public static String generateDefaultLongGuidHex() {
        return generateCheckSumCRC32Hex(generateDefaultGuid().getBytes(Charset.forName("UTF-8")));
    }

    public static String generateSimpleLongGuidHex() {
        return generateCheckSumCRC32Hex(generateSimpleGuid().getBytes(Charset.forName("UTF-8")));
    }

    public static long generateCheckSumCRC32(byte[] bytes) {
        Checksum checksum = new CRC32();
        checksum.update(bytes, 0, bytes.length);
        return checksum.getValue();
    }

    public static String generateCheckSumCRC32Hex(byte[] bytes) {
        Checksum checksum = new CRC32();
        checksum.update(bytes, 0, bytes.length);
        long lngChecksum = checksum.getValue();
        return Long.toHexString(lngChecksum);
    }

    public static String generate(String... fields) {
        if (fields != null && fields.length != 0) {
            StringBuilder sb = new StringBuilder();
            String[] var2 = fields;
            int var3 = fields.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String field = var2[var4];
                if (StringUtils.isBlank(field)) {
                    throw new RuntimeException("generate need fields.");
                }

                sb.append(generateCheckSumCRC32Hex(field.getBytes(Charset.forName("UTF-8"))));
            }

            return sb.toString();
        } else {
            throw new RuntimeException("generate need fields.");
        }
    }

    public static void main(String[] args) {
        System.out.println(generateSimpleGuid());
    }
}
